module Orator

  # A representation of a client.
  class Client

    # The event handler for this client.
    #
    # @return [EventHandler]
    attr_accessor :event_handler

    # The context to be used by the client.
    #
    # @return [Object]
    attr_accessor :context

    # The socket to be used to talk to the client.
    #
    # @return [#send]
    attr_accessor :socket

    # Initialize.
    #
    # @param data [Hash] the keys are used to set data on the client to the
    #   values of the hash.  If the attribute doesn't exist on the client,
    #   it is ignored.
    # @example
    #   client = Client.new :something => "foo"
    #   client.something # => "foo"
    def initialize(data)
      data.each do |k, v|
        send "#{k}=", v if respond_to? "#{k}="
      end
    end

    # This forwards the method call to [EventHandler#trigger], but removes an
    # argument: the context.
    #
    # @see [EventHandler#trigger]
    def trigger(event, *args)
      event_handler.trigger(event, context, *args)
    end

    # This is here just in case anyone gets the bright idea to serialize the
    # client with Oj.  This should not happen, because it could reveal
    # information that you may not want to be revealed.
    #
    # @return [Hash]
    def to_hash
      context.table
    end

  end
end
